package com.atguigu.yygh.hosp.controller.admin;


import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-11-05
 */
@Api(tags = "医院设置模块")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "查询医院设置列表", notes = "备注说明")
    @GetMapping("findAll")
    public Result findAll() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok().setData("list", list);
    }

    @ApiOperation("根据id删除")
    @DeleteMapping("{id}")
    public Result removeById(
            @ApiParam(value = "医院id", required = true, defaultValue = "1")
            @PathVariable Long id) {

        if (hospitalSetService.removeById(id)) {
            return Result.ok().setMessage("删除成功");
        } else {
            return Result.fail().setMessage("删除失败");
        }
    }

    @ApiOperation("分页条件查询")
    @GetMapping("/page/{current}/{limit}")
    public Result pageList(
            @ApiParam(value = "当页码", defaultValue = "1")
            @PathVariable Integer current,

            @ApiParam(value = "每页条数", defaultValue = "10")
            @PathVariable Integer limit,

            @ApiParam("查询条件对象")
                    HospitalSetQueryVo hospitalSetQueryVo) {

        Page<HospitalSet> hospitalSetPage = new Page<>(current, limit);

        hospitalSetPage = hospitalSetService.selectPage(hospitalSetQueryVo, hospitalSetPage);

        return Result.ok().setData("page", hospitalSetPage);
    }

    @ApiOperation("新增医院设置")
    @PostMapping("/save")
    public Result save(
            @RequestBody HospitalSet hospitalSet
    ) {
        //设置状态，数据库默认值为0，不可使用状态
        hospitalSet.setStatus(1);
        //设置签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        return hospitalSetService.save(hospitalSet) ? Result.ok() : Result.fail();
    }

    @ApiOperation("根据id查询")
    @GetMapping("/findById/{id}")
    public Result findById(
            @ApiParam(value = "医院id", required = true)
            @PathVariable Integer id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        int a = 1 / 0;
//        if (true)
//            throw new YyghException(2222, "自定义异常");
        return Result.ok().setData("item", hospitalSet);
    }

    @ApiOperation("根据id修改")
    @PutMapping("/update")
    public Result updateById(
            @ApiParam(value = "医院Id", required = true)
            @RequestBody HospitalSet hospitalSet
    ) {
        try {
            int a = 1 / 0;
        } catch (Exception e) {
            throw new YyghException(11111, "算术异常", e);
        }
        return hospitalSetService.updateById(hospitalSet) ? Result.ok() : Result.fail();
    }


}